package logic

import (
	"context"
	"fmt"
	"useryk/model/mysql"

	"useryk/userrpc/internal/svc"
	"useryk/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductAddLogic) ProductAdd(in *userrpc.ProductAddRequest) (*userrpc.ProductAddResponse, error) {
	// todo: add your logic here and delete this line

	product := mysql.Product{
		Categorieid:  in.Categorieid,
		Num:          in.Num,
		Name:         in.Name,
		Categorienum: in.Categorienum,
		Productprice: in.Productprice,
		Costprice:    in.Costprice,
		Image:        in.Image,
	}
	err := mysql.DB.Create(&product).Error
	if err != nil {
		return nil, fmt.Errorf("产品添加失败")
	}

	return &userrpc.ProductAddResponse{
		Productid: int64(product.ID),
	}, nil
}
